﻿using System;
using System.Web.UI;
using System.Data;
using Microsoft.Reporting.WebForms;
using Microsoft.SharePoint;

namespace DynamicSSRSReport.SPListDynamicSSRSReport
{
    public partial class SPListDynamicSSRSReportUserControl : UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                loadAllLists();
            }
        }

        private void loadAllLists()
        {
            try
            {
                ddlTables.Items.Clear();
                using (SPSite site = new SPSite(SPContext.Current.Web.Url.ToString()))
                {
                    foreach (SPWeb web in site.AllWebs)
                    {
                        foreach (SPList list in web.Lists)
                        {
                            ddlTables.Items.Add(new System.Web.UI.WebControls.ListItem(list.Title.ToString(), list.Title.ToString()));
                        }
                        web.Dispose();
                    }
                }
            }
            catch (Exception ex)
            {
                lblMsg.Text = "Error : " + ex.Message.ToString() + "     Stack Trace : " + ex.StackTrace.ToString();
            }
        }

        private DataTable ConvertSPListToDataTable(string spListName)
        {
            SPWeb oWebsite = SPContext.Current.Web;
            SPList oList = oWebsite.Lists[spListName];
            SPListItemCollection collListItems = oList.Items;
            return collListItems.GetDataTable();
        }

        protected void btnShowReport_Click(object sender, EventArgs e)
        {
            try
            {
                #region GetDatatable

                string tName = ddlTables.SelectedItem.Text.ToString();
                string dsName = ddlTables.SelectedValue;
                DataTable dt = ConvertSPListToDataTable(tName);

                for (int i = 0; i < dt.Columns.Count; i++)
                {
                    dt.Columns[i].ColumnName = ReportGenerator.FormatForDynamicRdlc(dt.Columns[i].ColumnName);
                }
                dt.AcceptChanges();

                #endregion

                #region RDLC
                try
                {
                    ReportGenerator gen = new ReportGenerator(dt, dsName);
                    ReportDataSource ds = new ReportDataSource(dsName, dt);
                    ReportViewer1.Reset();
                    ReportViewer1.LocalReport.DataSources.Add(ds);
                    ReportViewer1.LocalReport.DisplayName = dsName;
                    ReportViewer1.LocalReport.LoadReportDefinition(gen.GeneraReport());
                }
                catch (Exception ex)
                {
                    lblMsg.Text = "Error : " + ex.Message.ToString() + "     Stack Trace : " + ex.StackTrace.ToString();
                }
                #endregion

                #region Datatable
                //try
                //{
                //    gvListData.DataSource = dt;
                //    gvListData.DataBind();
                //}
                //catch (Exception ex)
                //{
                //    lblMsg.Text += "Error : " + ex.Message.ToString() + "     Stack Trace : " + ex.StackTrace.ToString();
                //}
                #endregion
            }
            catch (Exception ex)
            {
                lblMsg.Text = "Error : " + ex.Message.ToString() + "     Stack Trace : " + ex.StackTrace.ToString();
            }
        }
    }
}
